/**
 * 
 */
package com.vector.ding.commons.util.web;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

/**
 * @author vector
 *
 */
public class RequestUtil {
	
	public static JSONObject getParamJson(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		@SuppressWarnings("unchecked")
		Enumeration<String> keys = request.getParameterNames();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = request.getParameter(key);
			if (null == value) {
				String[] values = request.getParameterValues(key);
				json.put(key, values);
			} else {
				json.put(key, value);
			}
		}
		
		return json;
	}
	
	public static Map<String, Object> getParamMap(HttpServletRequest request) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		@SuppressWarnings("unchecked")
		Enumeration<String> keys = request.getParameterNames();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = request.getParameter(key);
			if (null == value) {
				String[] values = request.getParameterValues(key);
				paramMap.put(key, values);
			} else {
				paramMap.put(key, value);
			}
		}
		
		return paramMap;
	}
}
